package day9;

class Bus_t{
    int tickets = 80;

    int count = 1;

    public void show() throws InterruptedException {
        while (true){
            synchronized (Object.class) {

                    while (count != 1) {
                        Object.class.wait();
                    }
                if (tickets > 0) {
                    Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "车上剩余位置:" + (tickets--) + "个");
                }else {
                    break;
                }
                count = 2;
                Object.class.notifyAll();
            }
        }
    }


    public void show2() throws InterruptedException {
        while (true){
            synchronized (Object.class) {
                    while (count != 2) {
                        Object.class.wait();
                    }
                if (tickets > 0) {
                    Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "车上剩余位置:" + (tickets--) + "个");
                }else {
                    break;
                }
                count = 3;
                Object.class.notifyAll();
            }
        }
    }
    public void show3() throws InterruptedException {
        while (true){
            synchronized (Object.class) {
                while (count != 3) {
                        Object.class.wait();
                    }
                if (tickets > 0) {
                    Thread.sleep(10);
                    System.out.println(Thread.currentThread().getName() + "车上剩余位置:" + (tickets--) + "个");
                }else {
                    break;
                }
                count = 1;
                Object.class.notifyAll();
            }
        }
    }
}


public class Demo8 {
    public static void main(String[] args) {
        final Bus_t s = new Bus_t();
        new Thread("前门") {
            @Override
            public void run() {
                try {
                    s.show();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread("中门") {
            @Override
            public void run() {
                try {
                    s.show2();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();

        new Thread("后门") {
            @Override
            public void run() {
                try {
                    s.show3();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
}
